import { defineConfig, loadEnv, type Plugin } from 'vite';
import uni from '@dcloudio/vite-plugin-uni';
import AutoImport from 'unplugin-auto-import/vite';
import path from 'path';
// 强制 HMR 刷新
function forceRefresh(): Plugin {
  return {
    name: 'force-refresh',
    handleHotUpdate({ file, server }) {
      // 当任何 .ts 文件更新时，强制刷新页面
      if (file.endsWith('.ts') || file.endsWith('.vue')) {
        server.ws.send({
          type: 'full-reload',
          path: '*',
        });
        return [];
      }
    },
  };
}

interface ProxyConfig {
  target: string;
  changeOrigin?: boolean;
  rewrite?: (path: string) => string;
}

function buildProxies(source: Record<string, string>) {
  return Object.entries(source).reduce(
    (acc, [key, value]) => {
      acc[key] = {
        target: value,
        changeOrigin: true,
        rewrite: (path: string) => path.replace(new RegExp(`^${key}`), ''),
      };
      return acc as Record<string, ProxyConfig>;
    },
    {} as Record<string, ProxyConfig>
  );
}

export default defineConfig(async ({ mode }) => {
  // 加载环境变量
  const env = loadEnv(mode, process.cwd(), '');

  const UnoCss = await import('unocss/vite').then(i => i.default);

  return {
    base: '/',
    plugins: [
      uni(),
      UnoCss(),
      forceRefresh(),
      AutoImport({
        imports: ['vue', 'uni-app', 'pinia'],
        dirs: ['src/api', 'src/composables', 'src/config', 'src/store', 'src/utils'],
        dts: 'src/auto-imports.d.ts',
        // 解决eslint报错
        eslintrc: {
          enabled: true,
        },
      }),
    ],

    server: {
      host: '0.0.0.0',
      port: 3000,
      proxy: buildProxies({
        [env.VITE_AUTHOR_API_PREFIX]: env.VITE_AUTHOR_API_URL,
        [env.VITE_WEB_API_PREFIX]: env.VITE_WEB_API_URL,
        [env.VITE_WECHAT_API_PREFIX]: env.VITE_WECHAT_API_URL,
        [env.VITE_BIGDATA_API_PREFIX]: env.VITE_BIGDATA_API_URL,
        [env.VITE_FONT_PREFIX]: env.VITE_FONT_FAMILY_URL,
      }),
      // 解决微信小程序开发者工具热更新问题
      hmr: {
        overlay: false,
        clientPort: 3000,
      },
      watch: {
        usePolling: true,
        interval: 1000,
      },
      open: true,
    },

    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
      },
    },

    css: {
      preprocessorOptions: {
        scss: {
          implementation: require('sass'),
          additionalData: `@import "./src/uni.scss";`,
        },
      },
    },
  };
});
